package com.neusoft.resturant.controller;

import com.alibaba.fastjson2.JSON;
import com.neusoft.resturant.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

public class BaseController {
    @Autowired
    protected HttpServletRequest request;

    @Resource
    protected RedisTemplate redisTemplate;

    // 从request中获取用户id
    public Long getUserId() {
        return (Long) request.getAttribute("userId");
    }


    // 获取当前登录用户信息
    public SysUser getCurUser() {
        String token = request.getHeader("token");
        String userJson = (String) redisTemplate.opsForValue().get(token);
        SysUser curUser = JSON.parseObject(userJson,SysUser.class);
        return curUser;
    }
}
